package com.zzh.tree;

import java.util.ArrayDeque;
import java.util.Queue;

public class No117_connect {
    public static void main(String[] args) {

    }
}
class No117Solution{
    public Node connect(Node root) {
        Queue<Node> q=new ArrayDeque<>();
        q.offer(root);
        while (!q.isEmpty()){
            int size=q.size();
            for (int i=0;i<size;i++){
                Node cur=q.poll();
                assert cur!=null;
                if (i==size-1){
                    cur.next=null;
                }else{
                    cur.next=q.peek();
                }
                if (cur.left!=null){
                    q.offer(cur.left);
                }
                if (cur.right!=null){
                    q.offer(cur.right);
                }
            }
        }
        return root;
    }
}
